package com.lengyu.iottransfer.gateway.redis;

import com.lengyu.iottransfer.gateway.common.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * redis 共通类
 * @author liuyu
 * @since 2019/12/12 16:09
 */
@Component
public class RedisUtils {

    private final static Logger logger = LoggerFactory.getLogger(RedisUtils.class);

    public static final long EXPIRE_TIME = 60;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * <p>长久保存redis</>
     * @param key
     * @param value
     * @author liuyu
    **/
    public void set(String key, String value) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set(Const.REDIS_PREFIX + key, value);
    }

    /**
     * <p>持续一段时间保存redis</p>
     * @param key
     * @param value
     * @param time 持续时间
     * @param timeUnit 时间单位
     * @author liuyu
    **/
    public void set(String key, String value,long time,TimeUnit timeUnit) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set(Const.REDIS_PREFIX + key, value,time, timeUnit);
    }

    /**
     * <p>获取redis数据</p>
     * @param key
     * @return java.lang.String
     * @author liuyu
    **/
    public String get(String key) {
        ValueOperations<String, String> ops = this.redisTemplate.opsForValue();
        return ops.get(Const.REDIS_PREFIX + key);
    }

    /**
     * <p>长久保存redis</>
     * @param key
     * @param value
     * @author liuyu
     **/
    public void setLong(String key, long value) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String valueStr = String.valueOf(value);
        ops.set(Const.REDIS_PREFIX + key, valueStr);
    }

    /**
     * <p>持续一段时间保存redis</p>
     * @param key
     * @param value
     * @param time 持续时间
     * @param timeUnit 时间单位
     * @author liuyu
     **/
    public void setLong(String key, Long value,long time,TimeUnit timeUnit) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String valueStr = String.valueOf(value);
        ops.set(Const.REDIS_PREFIX + key, valueStr,time, timeUnit);
    }

    public Long getLong(String key){
        try{
            ValueOperations<String, String> ops = this.redisTemplate.opsForValue();
            String value = ops.get(Const.REDIS_PREFIX + key);
            long valueLong = Long.parseLong(value);
            return valueLong;
        }
        catch(Exception ex){
            return null;
        }

    }

    /**
     * <p>删除redis数据</p>
     * @param key
     * @author liuyu
     **/
    public void delete(String key) {
        this.redisTemplate.delete(Const.REDIS_PREFIX + key);
    }

    /**
    *发布消息主题
    *@param topic
*@param message
    *@return void
    *@author liuyu
    **/
    public void convertAndSend(String topic,String message){
        this.redisTemplate.convertAndSend(topic,message);
    }
}
